package com.icode.core.shared;

import com.icode.core.dto.about.*;
import com.icode.core.model.about.*;
import com.icode.core.model.house.HomeInfoImage;
import com.icode.core.model.house.HomeIntroImage;
import com.icode.core.model.house.HomePartnerImage;
import com.icode.service.about.HomeAboutService;
import com.icode.service.house.HomePageService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-3-9
 * Time: 上午9:42
 * To change this template use File | Settings | File Templates.
 */
public class HomeAboutHandle {

    public static Introduce toIntroduce(String title, String data) {
        String[] datas = data.split("\\^");
        SubHead subHead;
        SubContent subContent;
        List<SubHead> subHeads = new ArrayList<>();
        List<SubContent> subContents;
        Introduce introduce = new Introduce(title);
        for (int i = 0; i < datas.length; i++) {
            subContents = new ArrayList<>();
            String[] contents = datas[i].split(";");
            subHead = new SubHead(contents[0], introduce, subContents);
            String[] content = contents[1].split(",");
            for (int j = 0; j < content.length; j++) {
                subContent = new SubContent(content[j], subHead);
                subContents.add(subContent);
            }
            subHeads.add(subHead);
        }
        introduce.initSummarize(subHeads);
        return introduce;
    }

    public static Model toModel(String data) {
        String[] datas = data.split("\\^");
        SubHead subHead;
        SubContent subContent;
        List<SubHead> subHeads = new ArrayList<>();
        List<SubContent> subContents;
        Model model = new Model();
        for (int i = 0; i < datas.length; i++) {
            subContents = new ArrayList<>();
            String[] contents = datas[i].split(";");
            subHead = new SubHead(contents[0], model, subContents);
            String[] content = contents[1].split(",");
            for (int j = 0; j < content.length; j++) {
                subContent = new SubContent(content[j], subHead);
                subContents.add(subContent);
            }
            subHeads.add(subHead);
        }
        model.initSummarize(subHeads);
        return model;
    }

    public static Concept toConcept(String data) {
        String[] datas = data.split("\\^");
        SubHead subHead;
        SubContent subContent;
        List<SubHead> subHeads = new ArrayList<>();
        List<SubContent> subContents;
        Concept concept = new Concept();
        for (int i = 0; i < datas.length; i++) {
            subContents = new ArrayList<>();
            String[] contents = datas[i].split(";");
            subHead = new SubHead(contents[0], concept, subContents);
            String[] content = contents[1].split(",");
            for (int j = 0; j < content.length; j++) {
                subContent = new SubContent(content[j], subHead);
                subContents.add(subContent);
            }
            subHeads.add(subHead);
        }
        concept.initSummarize(subHeads);
        return concept;
    }

    public static Cooperation toCooperation(String data) {
        String[] datas = data.split("\\^");
        SubHead subHead;
        SubContent subContent;
        List<SubHead> subHeads = new ArrayList<>();
        List<SubContent> subContents;
        Cooperation cooperation = new Cooperation();
        for (int i = 0; i < datas.length; i++) {
            subContents = new ArrayList<>();
            String[] contents = datas[i].split(";");
            subHead = new SubHead(contents[0], cooperation, subContents);
            String[] content = contents[1].split(",");
            for (int j = 0; j < content.length; j++) {
                subContent = new SubContent(content[j], subHead);
                subContents.add(subContent);
            }
            subHeads.add(subHead);
        }
        cooperation.initSummarize(subHeads);
        return cooperation;
    }

    public static List<News> toNews(String data) {
        String[] dates = data.split(";");
        List<News> newses = new ArrayList<>();
        List<NewsSource> newsSources;
        for (int i = 0; i < dates.length; i++) {
            newsSources = new ArrayList<>();
            String[] contents = dates[i].split("\\*");
            String[] content = contents[0].split("\\^");
            News news = new News(content[0], content[1]);
            if (contents.length > 1) {
                String[] sources = contents[1].split("~");
                for (int j = 0; j < sources.length; j++) {
                    String[] source = sources[j].split("\\^");
                    NewsSource newsSource = new NewsSource(source[0], source[1], news);
                    newsSources.add(newsSource);
                }
            }
            news.initNewsSource(newsSources);
            newses.add(news);
        }
        return newses;
    }

    public static void showByType(String type, HomeAboutService homeAboutService, org.springframework.ui.Model model) {
        if ("introduce".equals(type)) {
            IntroduceDTO introduceDTO = homeAboutService.findIntroduce();
            model.addAttribute("objectDTO", introduceDTO);
            model.addAttribute("type", type);
        } else if ("model".equals(type)) {
            ModelDTO modelDTO = homeAboutService.findModel();
            model.addAttribute("objectDTO", modelDTO);
            model.addAttribute("type", type);
        } else if ("concept".equals(type)) {
            ConceptDTO conceptDTO = homeAboutService.findConcept();
            model.addAttribute("objectDTO", conceptDTO);
            model.addAttribute("type", type);
        } else if ("cooperation".equals(type)) {
            CooperationDTO cooperationDTO = homeAboutService.findCooperation();
            model.addAttribute("objectDTO", cooperationDTO);
            model.addAttribute("type", type);
        } else if ("news".equals(type)) {
            List<NewsDTO> newsDTOs = homeAboutService.findNewsBySort();
            model.addAttribute("objectDTO", newsDTOs);
            model.addAttribute("type", type);
        }
    }

    public static void delHomePageImg(String type, String imgGuid, HomePageService homePageService) {
        if ("intro".equals(type)) {
            HomeIntroImage homeIntroImage = homePageService.findIntroByImgGuid(imgGuid);
            homePageService.deleteObject(homeIntroImage);
        } else if ("info".equals(type)) {
            HomeInfoImage homeInfoImage = homePageService.findInfoByImgGuid(imgGuid);
            homePageService.deleteObject(homeInfoImage);
        } else if ("partners".equals(type)) {
            HomePartnerImage homePartnerImage = homePageService.findPartnersByImgGuid(imgGuid);
            homePageService.deleteObject(homePartnerImage);
        }
    }

    public static void homeAboutForm(String title, String data, String type, String id, HomeAboutService homeAboutService) {
        if ("introduce".equals(type)) {
            Introduce introduce = HomeAboutHandle.toIntroduce(title, data);
            if (!"".equals(id)) {
                homeAboutService.deleteSubByIntroduceId(id);
                introduce.setId(Integer.parseInt(id));
            }
            homeAboutService.saveOrUpdateIntroduce(introduce);
        } else if ("model".equals(type)) {
            Model model = HomeAboutHandle.toModel(data);
            if (!"".equals(id)) {
                homeAboutService.deleteSubByModelId(id);
                model.setId(Integer.parseInt(id));
            }
            homeAboutService.saveOrUpdateModel(model);
        } else if ("concept".equals(type)) {
            Concept concept = HomeAboutHandle.toConcept(data);
            if (!"".equals(id)) {
                homeAboutService.deleteSubByConceptId(id);
                concept.setId(Integer.parseInt(id));
            }
            homeAboutService.saveOrUpdateConcept(concept);
        } else if ("cooperation".equals(type)) {
            Cooperation cooperation = HomeAboutHandle.toCooperation(data);
            if (!"".equals(id)) {
                homeAboutService.deleteSubByCooperationId(id);
                cooperation.setId(Integer.parseInt(id));
            }
            homeAboutService.saveOrUpdateCooperation(cooperation);
        } else if ("news".equals(type)) {
            List<News> news = HomeAboutHandle.toNews(data);
            homeAboutService.deleteAllNews();
            homeAboutService.saveOrUpdateNews(news);
        }
    }

}
